package com.aaa.doctor.service.impl;

import com.aaa.doctor.feign.system.UserFeign;
import com.aaa.doctor.mapper.CareHistoryMapper;
import com.aaa.doctor.mapper.CareOrderItemMapper;
import com.aaa.doctor.mapper.CareOrderMapper;
import com.aaa.doctor.service.CareHistoryService;
import com.aaa.doctor.utils.IdWorker;
import com.aaa.entity.CareHistory;
import com.aaa.entity.CareOrder;
import com.aaa.entity.CareOrderItem;
import com.aaa.utils.JwtUtil;
import com.aaa.utils.WebUtil;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Random;


/**
 * @author MuChengGuo
 * @date 2023/3/25 9:08
 * Description: TODO
 **/
@Service
public class CareHistoryServiceImpl implements CareHistoryService {
    @Autowired(required = false)
    private CareHistoryMapper careHistoryManager;
    @Autowired(required = false)
    private CareOrderMapper careOrderMapper;
    @Autowired(required = false)
    private CareOrderItemMapper careOrderItemMapper;
    @Autowired
    private UserFeign userFeign;


    /**
     * 通过患者ID(patientId)查询患者病例信息
     *
     * @param patientId
     * @return
     */
    @Override
    public Result<List<CareHistory>> getListByPatientId(String patientId) {
        QueryWrapper<CareHistory> wrapper = new QueryWrapper<>();
        wrapper.eq("patient_id", patientId);
        List<CareHistory> careHistory = careHistoryManager.selectList(wrapper);

        for (CareHistory history : careHistory) {
            QueryWrapper<CareOrder> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("ch_id", history.getChId());
            List<CareOrder> careOrders = careOrderMapper.selectList(wrapper1);
            history.setCareOrderList(careOrders);
            for (CareOrder careOrder : careOrders) {
                QueryWrapper<CareOrderItem> wrapper2 = new QueryWrapper<>();
                wrapper2.eq("co_id", careOrder.getCoId());
                List<CareOrderItem> careOrderItems = careOrderItemMapper.selectList(wrapper2);
                careOrder.setCareOrderItemList(careOrderItems);
            }
        }
        return new Result<List<CareHistory>>(2000, "", careHistory);
    }

    /**
     * 保存病历本
     *
     * @param careHistory
     * @return
     */
    @Override
    public Result<String> saveCareHistory(CareHistory careHistory) {
        HttpServletRequest request = WebUtil.getRequest();
        String phone = (String) JwtUtil.getTokenChaim(request.getHeader("token")).get("username");

        Long userId = userFeign.getByUsername(phone).getUserId();
        careHistory.setUserId(userId);
        String userName = userFeign.getByUsername(phone).getUserName();
        careHistory.setUserName(userName);
        Long deptId = userFeign.getByUsername(phone).getDeptId();
        careHistory.setDeptId(deptId);
        String deptName = userFeign.getByDeptId(deptId).getDeptName();
        careHistory.setDeptName(deptName);

        careHistory.setCareTime(new Date());
        IdWorker idWorker = new IdWorker();
        int i = new Random().nextInt(89) + 10;
        StringBuilder ch = new StringBuilder().append("CH").append(idWorker.nextId()).append(i);
        careHistory.setChId(String.valueOf(ch));

        careHistoryManager.insert(careHistory);

        return new Result<String>(2000, "添加成功", careHistory.getChId());
    }
}
